/**
 * 表格通用
 * author xty
 */

//获取表格选中数据
function getSelectedRows(form) {
    if(form&&form.length>0){
      return $(form).bootstrapTable('getSelections', function (row) {
          return row;
      });
    }
    return null;
}

// bootStrap-treeView 通用初始化方法、select与check一致 ;返回所有节点Ids
function initTreeView(tree,data,selectedData,flags){
    if(tree&&tree.length>0){
        $(tree).treeview({
            showCheckbox: true,
            multiSelect: true,
            data:data?data:"",
            onNodeSelected:function(event, data){
                nodeSelected(tree,data);
            },
            onNodeUnselected:function(event, data){
                nodeUnselected(tree,data);
                },
            onNodeChecked:function(event, data){
                nodeChecked(tree,data,flags);
            },
            onNodeUnchecked:function(event, data){
                nodeUnchecked(tree,data,flags);
            }
        });
        //初始化数据
        return initTreeData(tree,selectedData);
    }
}
function initTreeViewReadOnly(tree,data,selectedData,flags) {
    if (tree && tree.length > 0) {
        $(tree).treeview({
            showCheckbox: false,
            multiSelect: false,
            data: data ? data : ""
        });
        //初始化数据
        return initTreeDataReadOnly(tree, selectedData);
    }
}
function initTreeDataReadOnly(tree,selectedData){
    var _nodeIds=new Array();
    //初始化数据
    var allNodes=$(tree).treeview('getUnselected');
    if(allNodes&&allNodes.length>0){
        $.each(allNodes,function(i,node){
            _nodeIds.push(node.id);
            if(selectedData&&selectedData.length>0){
                if($.inArray(node.id-0,selectedData)!=-1){
                    node.color='#FFFFFF';
                    node.backColor='#428bca';
                    var pNode=$(tree).treeview('getParent', node);
                    if(pNode){
                        pNode.color='#FFFFFF';
                        pNode.backColor='#428bca';
                    }
                }
            }
            node.selectable=false;
        });
    }
    $(tree).treeview('expandAll', {silent: true });
    return _nodeIds;
}
//废弃不用会造成点击页面卡死
function disableTreeView(tree){
    if(tree&&tree.length>0){
        $('#tree').on('nodeSelected',function(event, data) {
            $(tree).treeview('unselectNode',data.nodeId,{silent:true});
        });
        $('#tree').on('nodeUnselected ',function(event, data) {
            $(tree).treeview('selectNode',data.nodeId,{silent:true});
        });
        $('#tree').on('nodeChecked ',function(event, data) {
            $(tree).treeview('uncheckNode',data.nodeId,{silent:true});
        });
        $('#tree').on('nodeUnchecked ',function(event, data) {
            $(tree).treeview('checkNode',data.nodeId,{silent:true});
        });
    }
}
//点击节点事件
function nodeSelected(tree,data) {
    $(tree).treeview('checkNode',data.nodeId,{silent:true});
}

//点击节点事件
function nodeUnselected(tree, data) {
    $(tree).treeview('uncheckNode',data.nodeId,{silent:true});
}
//选中节点事件
function nodeUnchecked(tree, data,flags) {
    $(tree).treeview('unselectNode',data.nodeId,{silent:true});
    if(flags&&!flags.nodeUncheckedSilent) {
        flags.nodeUncheckedSilent = true;
        //子全空则清空父节点
        if(data&&data.level==2){
            var pNode=$(tree).treeview('getParent', data);
            if(isChildrenAllUnchecked(pNode)){//全空
                $(tree).treeview('uncheckNode',pNode,{silent:true});
            }
        }else if(data&&data.level==1){
            //清空子节点
            var selectNodes = getChildNodeIdArr(data); //获取所有子节点
            if (selectNodes) { //子节点不为空，则取消选中所有子节点
                $(tree).treeview('uncheckNode', [selectNodes, {silent: true}]);
                $(tree).treeview('unselectNode', [selectNodes, {silent: true}]);
            }
        }
        flags.nodeUncheckedSilent=false;
    }
}



//选中节点事件
function nodeChecked(tree, data,flags) {
    $(tree).treeview('selectNode',data.nodeId,{silent:true});
    if(flags&&!flags.nodeCheckedSilent){
        flags.nodeCheckedSilent=true;
        //选中父节点
        if(data&&data.level==2){
            var pNode=$(tree).treeview('getParent', data);
            $(tree).treeview('checkNode',pNode,{silent:true});
        }else if(data&&data.level==1){
            //选中子节点
            var selectNodes = getChildNodeIdArr(data); //获取所有子节点
            if (selectNodes) { //子节点不为空，则取消选中所有子节点
                $(tree).treeview('checkNode', [selectNodes, { silent: true }]);
                $(tree).treeview('selectNode', [selectNodes, { silent: true }]);
            }
        }
        flags.nodeCheckedSilent=false;
    }
}

function initTreeData(tree,selectedData){
    var _nodeIds=new Array();
    //初始化数据
    var allNodes=$(tree).treeview('getUnselected');
    if(allNodes&&allNodes.length>0){
        $.each(allNodes,function(i,node){
            _nodeIds.push(node.id);
            if(selectedData&&selectedData.length>0){
                if($.inArray(node.id-0,selectedData)!=-1){
                    if(node.level==2){
                        $(tree).treeview('checkNode',node,{silent:true});
                        $(tree).treeview('selectNode',node,{silent:true});
                    }
                }
            }
        });
    }
    $(tree).treeview('collapseAll', {silent: true });
  return _nodeIds;
}

function  getSelectedNodeIds(tree,p_level) {
    var _nodeIds=new Array();
    var allNodes=$(tree).treeview('getSelected');
    if(allNodes&&allNodes.length>0){
        $.each(allNodes,function(i,node) {
            if(!p_level||node.level==p_level){
                _nodeIds.push(node.id);
            }
        });
    }
    return _nodeIds;
}

/**
 * @Description: 操作分隔符
 * @Return:
 * @Param:
 * @Author: caiyl@hsyuntai.com
 * @Date: 2018/1/19 10:50
 */
function getSeparator() {
    return '<span class="ant-divider"></span>';
}

//初始化列表高度
function initTableHeight(_hx) {
    var height = $(window).height() - _hx;
    $('.fixed-table-container').css('height', height+'px');
}
//获取所有子节点
function getChildNodeIdArr(node) {
    var ts = [];
    if (node.nodes) {
        for (x in node.nodes) {
            ts.push(node.nodes[x].nodeId);
        }
    }
    return ts;
}
//判断子节点都未选中
function isChildrenAllUnchecked(node) {
    var res=true;
    if (node.nodes){
        for (x in node.nodes) {
           if(node.nodes[x].state.checked){
               res=false;
               break;
           }
        }
    }
    return res;
}

/**
* @Description: 查询条件传递前
* @author maoze@hsyuntai.com
* @date 2018/6/20 14:07
* @params No such property: code for class: Script1
* @return
* Modified By:
*/

function searchStrBefore(from,dataTable) {
    var searchStr = $(from).serialize();
    if(dataTable != null){
        var tableOptions = $(dataTable).bootstrapTable('getOptions', null);
        searchStr += '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
    }
    console.log(searchStr);
    if(searchStr.length == 0){
        return "";
    }else{
        return ","+searchStr.replace(/&/g,",");
    }
}

/**
* @Description: searchfrom的条件
* @author maoze@hsyuntai.com
* @date 2018/6/20 18:42
* @params No such property: code for class: Script1
* @return
* Modified By:
*/
// function searchStrBefore(from) {
//     var searchStr = $(from).serialize();
//     if(searchStr.length == 0){
//         return "";
//     }else{
//         return ","+searchStr.replace(/&/g,",");
//     }
// }


/**
* @Description:查询条件传递后
* @author maoze@hsyuntai.com
* @date 2018/6/20 14:10
* @params No such property: code for class: Script1
* @return
* Modified By:
*/

function searchStrAfter(search) {
    return search.replace(/,/g,"&");
}